var baseurl = '';
//var baseurl="https://www.shhyxypsx.com";
//var baseurl="https://m.sfddj.com";

var smallImgArg = '?x-oss-process=style/goods_img_3';
var smallImgArg300 = '?x-oss-process=style/goods_img_2';
var smallImgArg200 = '?x-oss-process=style/goods_img';


define(function(require,exports,module) {
	//yx
	//var httpImageUrl="https://fm95338bucket.oss-cn-shenzhen.aliyuncs.com/";
    //zs
	//var httpImageUrl="https://sfddj-img-bucket.oss-cn-shenzhen.aliyuncs.com/";
    var httpImageUrl
    if(window.location.host == 'm.sfddj.com' || window.location.host == '120.76.47.184' || window.location.host == '120.76.207.85' || window.location.host == '120.76.44.28') {
        httpImageUrl="https://sfddj-img-bucket.oss-cn-shenzhen.aliyuncs.com/";
    }
    else if (window.location.host == 'shop.fx-sf.com') {
        httpImageUrl="https://fm95338bucket.oss-cn-shenzhen.aliyuncs.com/";
    }
    else if(window.location.hostname == 'localhost') {
        httpImageUrl="http://img-dev.sfddj.com/";
    }
	exports.httpImageUrl = httpImageUrl;

    //加入购物车的动画
    function AddCarAnimate(_X,_Y,target,sLeft,sTop,offsetDis,callBack) {
            // var _X = e.gesture.center.x;
            // var _Y = e.gesture.center.y;
            var time = null;
            var timeTwo = null;
            target.removeClass('myRotateFast');
            target.css({ 'left':_X,'top':_Y, 'opacity':1});
            clearTimeout(time);
            time = setTimeout(function() {
                target.addClass('myRotateFast').stop().animate({'left':_X,'top':_Y-50, 'opacity':1},300,function() {
                    target.stop().animate({'left':sLeft,'top':sTop},600,function() {
                        target.removeClass('myRotateFast');
                        clearTimeout(timeTwo);
                        timeTwo = setTimeout(function() {
                        	target.stop().animate({'top':sTop + offsetDis*2, 'opacity':0},200,function() {
                        		if(callBack) callBack();
                        	})
                        },200)
                    });
                });

            },30);
            // HamstopPropaga();
            // e.stopPropagation();
            // e.preventDefault()
    }
    exports.AddCarAnimate = AddCarAnimate;

    // hammer 冒泡
    function HamstopPropaga() {
        window.event.returnValue = false;
        return false;
    }
    exports.HamstopPropaga = HamstopPropaga;

    //hammer事件
    function HammerEvent(obj) {
        this.obj = obj;
    }

    HammerEvent.prototype.addHamEvent = function(obj,type,fn) {
        this.obj = new Hammer.Manager(obj);
        this.obj.add(new Hammer.Swipe({threshold:8,direction:Hammer.DIRECTION_ALL}));
        this.obj.add(new Hammer.Tap({}));
        this.obj.add(new Hammer.Press({velocity:0.6,threshold:8})); //长时间下压
        this.obj.on(type,function(e) {
            if(fn) fn(e);
            //e.preventDefault();
            //e.stopPropagation();
        });
    };

    exports.HammerEvent = HammerEvent;


     /* 数字验证 */
    function ValiNum(obj,fn1,fn2) {
        var reg = new RegExp("^[0-9]*$");
        if(!reg.test(obj.val())){
            if(fn1) fn1();
        }
        else if( obj.val() < 0) {
            TipLayer("数量不能小余1");
            obj.val(1);
        }
        else{if(fn2) fn2();}
    }
    exports.ValiNum = ValiNum;

    /* 字数统计 */
    function statInputNum(textArea,numItem) {
        var max =  numItem.eq(0).text();
        var curLength = 0;
            textArea.each(function(i) {
                textArea.eq(i).attr("maxlength", max).on('input propertychange', function () {

                    curLength = $(this).val().length;
                    numItem.eq(i).empty().text(max - curLength);
                 });
            });

    }
    exports.statInputNum = statInputNum;


    /* 购物车价格统计 */
    function ShopSumPrice(obj,tarFather,target,sumPrice) {
        var _price = 0;
        obj.each(function(i) {
            if(obj.eq(i).hasClass('active')) {
                var _oGoodsPrice = obj.eq(i).parents(tarFather).find(target);
                _oGoodsPrice.each(function(i) {
                    _price += (_oGoodsPrice.eq(i).text())*1;
                    return  _price;
                });
            }

        });
        sumPrice.text(_price.toFixed(2));
    }
    exports.ShopSumPrice = ShopSumPrice;


    /*************** 弹框效果 s***************/

    //提示框
    function TipLayer (sCont,sStyle,fn) {
        layer.open({
            content: sCont,
            style:sStyle || 'background-color:rgba(255,255,255,1); color:#252525; border:none; border-radius: 5px; min-width:50%',
            scrollbar: false,
            time: 2,
            shadeClose: false,
            success:function() {if(fn)fn();}
        });
        NoTouchMove($('.laymshade'))
    }
    exports.TipLayer = TipLayer;

    //框消失后执行回调提示框
    function TipLayerEnd (sCont,sStyle,fn,T) {
        layer.open({
            content: sCont,
            style:sStyle || 'background-color:rgba(255,255,255,1); color:#252525; border:none; border-radius: 5px;',
            scrollbar: false,
            time: T || 2,
            shadeClose: false,
            end:function() {if(fn)fn();}
        });
        NoTouchMove($('.laymshade'))
    }
    exports.TipLayerEnd = TipLayerEnd;


    function TipLayerLong (sCont,sStyle) {
        layer.open({
            content: sCont,
            style:sStyle || 'background-color:rgba(255,255,255,1); color:#252525; border:none; border-radius: 5px;',
            scrollbar: false,
            shadeClose: false,
        });
        NoTouchMove($('.laymshade'))
    }
    exports.TipLayerLong = TipLayerLong;

    //带按钮提示框
    function TipBtnLayer (sCont,sTit,btnName,yesFn,cancelFn,fn) {
        layer.open({
            title: sTit != 'no' ? sTit : '',
            content: sCont,
            btn: btnName == undefined ? ['确定'] : [btnName],
            shadeClose: false,
            scrollbar: false,
            yes:function() {if(yesFn) yesFn();},
            cancel:function() {if(cancelFn) cancelFn();},
            success:function() {if(fn)fn();}
        });
        NoTouchMove($('.laymshade'))
    }
    exports.TipBtnLayer = TipBtnLayer;

    // 标题提示框
    function TipTitleLayer (sTitle,styleTit,sCont,fn) {
        layer.open({
            title: [ sTitle,'background-color:#fff; color:#666; font-weight:bold' ],
            content: sCont,
            shadeClose:false,
            scrollbar: false,
            style: styleTit,
            cancel:function() {if(fn) fn();}

        });
        NoTouchMove($('.laymshade'))
    }
    exports.TipTitleLayer = TipTitleLayer;

    //询问层
    function askLayer(sCont,yesFn, noFn,sTit,callBack, aBtnName) {
        layer.open({
            //no 为没有标题 sTit为设置标题
            title:sTit != 'no' ? sTit : '',
            content: sCont,
            btn: aBtnName == undefined ? ['确认', '取消'] : aBtnName,
            shadeClose: false,
            scrollbar: false,
            yes: yesFn,
            no: noFn,
            cancel:noFn,
            success:function() {
                if(callBack) callBack();
            }

        });
        NoTouchMove($('.laymshade'))
    }
    exports.askLayer = askLayer;

    // 关闭所有弹框
    function LayerCloseAll() {
        layer.closeAll();
    }
    exports.LayerCloseAll = LayerCloseAll;

    //加载层
    function loadLayer(cont) {
        layer.open({type: 2,content:cont,shadeClose: false,});
        NoTouchMove($('.laymshade'))
    }
    exports.loadLayer = loadLayer;


    //防止滑动
    function NoTouchMove (obj) {
    	obj.on('touchmove',function() {return false;})
    }
    exports.NoTouchMove = NoTouchMove;


    /*************** 弹框效果 e***************/

    //节流控制
    function throttle(method, context,time) {
    	time == undefined ? time = 200 : time = time;
    	//console.log(time)
        clearTimeout(method.tId);
        method.tId = setTimeout(function () {
            method.call(context);
            //console.log(111)
        }, time);
    }
    exports.throttle = throttle;



    //s20是代表20秒 h是指小时，如12小时则是：h12 d是天数，30天则：d30
    //写cookies setCookie("name","hayden",'s20');
    function setCookie(name,value,time) {
        var strsec;
        if (time == undefined) {
            var Days = 30; //默认30天有效期
            strsec = Days*24*60*60*1000;
        }
        else {
            strsec = getsec(time)*1;
        }
        var exp = new Date();
        exp.setTime(exp.getTime() + strsec);
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString() + ";path=/";
    }
    exports.setCookie = setCookie;

    //读取cookies getCookie("name")
    function getCookie(name) {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg)) {
            return unescape(arr[2]);
        }
        else{
            return null;
        }

    }
    exports.getCookie = getCookie;

    //删除cookies delCookie('name')
    function delCookie(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval=getCookie(name);
        if(cval!=null)
        document.cookie = name + "="+cval+";expires="+exp.toGMTString() + ";path=/";
    }
    exports.delCookie = delCookie;

    //设置cookie时间
    function getsec(str) {
        var str1=str.substring(1,str.length)*1;
        var str2=str.substring(0,1);
        if (str2=="s") {
            return str1*1000;
        }
        else if (str2=="h") {
            return str1*60*60*1000;
        }
        else if (str2=="d") {
            return str1*24*60*60*1000;
        }
    }

    /* 验证码倒计时 */
    function CutTime(time,fn1,fn2) {
        var _AllTime = time;
        var _T = null;
        clearInterval(_T);
        _T = setInterval(function() {
            if(time<=0) {
                clearInterval(_T);
                if(fn1)fn1(_AllTime);
            }
            else {
                time--;
                if(fn2)fn2(time);
            }
        },1000)
    }
    exports.CutTime = CutTime;

    /*手机号码验证*/
    function checkMobile(obj,fn1,fn2,fn3){
        var oIphone = obj.val();
        var reg = /^(1[1|2|3|4|5|6|7|8|9])[\d]{9}$/;   //验证手机号码

        if(oIphone == '') {
            if(fn1)fn1()
        }
        else if (!reg.test(oIphone) ){

            if(fn2)fn2();
        }
        else {
            if(fn3)fn3();
        }
    }
    exports.checkMobile = checkMobile;


    /*密码验证*/
    function checkPassWord(obj,fn1,fn2,fn3){
        var _val = obj.val();
        var reg = /^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]{6,20}$/;//6-20位数字和英文
        if(_val == '') {
            if(fn1)fn1();
        }
        else if (!reg.test(_val)){
            if(fn2)fn2();
        }
        else {
            if(fn3)fn3();
        }
    }
    exports.checkPassWord = checkPassWord;


    /*用户名验证*/
    function checkUserName(obj,fn1,fn2,fn3){
        var _userName = obj.val();
        var reg = /^[A-Za-z\u4e00-\u9fa5][A-Za-z0-9\u4e00-\u9fa5]{1,9}$/;   //验证用户名

        if(_userName == '') {
            if(fn1)fn1()
        }
        else if (!reg.test(_userName) ){

            if(fn2)fn2();
        }
        else {
            if(fn3)fn3();
        }
    }

    exports.checkUserName = checkUserName;



	//图片预加载
	function lazLoadingImgEcho(fn) {
    	echo.init({
            offset : 300,
            throttle : 50, //设置图片延迟加载的时间
            callback: function (element, op) {
            	if(fn)fn()
		    	//console.log(element, 'has been', op + 'ed')
		    }
        });

    }
	exports.lazLoadingImgEcho = lazLoadingImgEcho;


	//图片预加载 刷新
	function EchoRush() {
		echo.render()
    }
	exports.EchoRush = EchoRush;

	//mui插件事件开关
	function OffMuiEvent(arg) {
    	mui.init({
			gestureConfig:{
				tap: arg, 	//默认为true
				swipe: arg, //默认为true
			}
		});
    }
	exports.OffMuiEvent = OffMuiEvent;



	//设置li图片高度
	function SetImgHeight(obj) {
  		var imgWidth = obj.eq(0).find('li figure').eq(0).width();
  		obj.find('li figure').css({'height': imgWidth, 'width': imgWidth});
  	}
	exports.SetImgHeight = SetImgHeight;


    //XSS 前端过滤
    function XssCheck(str,reg){
        return str ? str.replace(reg ||/[&<">'](?:(amp|lt|quot|gt|#39|nbsp|#\d+);)?/g,function (a, b) {
            if(b){
                return a;
            }else{
                return{
                    '<':'&lt;',
                    '&':'&amp;',
                    '"':'&quot;',
                    '>':'&gt;',
                    "'":'',
                }[a]
            }
        }): '';
    }
    exports.XssCheck = XssCheck;

    // 日期 2015-09-01 11:16:00
    function FormatDateTime (date, minType) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        minute = minute < 10 ? ('0' + minute) : minute;
        var seconds = date.getSeconds();
        seconds = seconds < 10 ? ('0' + seconds) : seconds;


        // 最小到天
        if(minType == 'minDay') {
            return y + '-' + m + '-' + d
        }
        // 最小到分钟
        else if (minType == 'minMinute') {
            return y + '-' + m + '-' + d + ' '+ h + ':' + minute
        }
        // 最小到秒
        else {
            return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + seconds;
        }
    }

    exports.FormatDateTime = FormatDateTime;



});


